from PyQt6.QtWidgets import (QMainWindow, QWidget, QVBoxLayout, QPushButton,
                           QStackedWidget, QLabel, QHBoxLayout)
from PyQt6.QtCore import Qt
from src.modules.img_background_remover import BackgroundRemover
from src.modules.img_split_processor import ImageProcessor

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("图片工具")
        self.init_ui()
    
    def init_ui(self):
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        
        # 创建水平布局作为主布局
        main_layout = QHBoxLayout(central_widget)
        main_layout.setContentsMargins(0, 0, 0, 0)
        main_layout.setSpacing(0)
        
        # 创建左侧导航面板
        nav_panel = QWidget()
        nav_panel.setFixedWidth(200)  # 固定宽度
        nav_panel.setStyleSheet("""
            QWidget {
                background-color: #f5f5f5;
                border-right: 1px solid #ddd;
            }
        """)
        nav_layout = QVBoxLayout(nav_panel)
        nav_layout.setContentsMargins(10, 20, 10, 20)
        nav_layout.setSpacing(10)
        
        # 添加标题
        title = QLabel("图片工具集")
        title.setStyleSheet("""
            QLabel {
                font-size: 18px;
                font-weight: bold;
                color: #333;
                padding: 10px;
            }
        """)
        title.setAlignment(Qt.AlignmentFlag.AlignCenter)
        nav_layout.addWidget(title)
        
        # 创建功能按钮样式
        button_style = """
            QPushButton {
                font-size: 14px;
                padding: 12px;
                background-color: transparent;
                border: none;
                border-radius: 4px;
                text-align: left;
            }
            QPushButton:hover {
                background-color: #e0e0e0;
            }
            QPushButton:checked {
                background-color: #ddd;
                font-weight: bold;
            }
        """
        
        # 图片分割按钮
        splitter_btn = QPushButton("图片分割")
        splitter_btn.setCheckable(True)
        splitter_btn.setStyleSheet(button_style)
        splitter_btn.clicked.connect(lambda: self.show_page(0, splitter_btn))
        nav_layout.addWidget(splitter_btn)
        
        # 背景移除按钮
        remover_btn = QPushButton("背景移除")
        remover_btn.setCheckable(True)
        remover_btn.setStyleSheet(button_style)
        remover_btn.clicked.connect(lambda: self.show_page(1, remover_btn))
        nav_layout.addWidget(remover_btn)
        
        # 保存按钮引用
        self.nav_buttons = [splitter_btn, remover_btn]
        
        # 添加导航栏底部弹簧
        nav_layout.addStretch()
        
        # 创建右侧内容区域
        content_panel = QWidget()
        content_panel.setStyleSheet("background-color: #f5f5f5;")
        content_layout = QVBoxLayout(content_panel)
        content_layout.setContentsMargins(20, 20, 20, 20)
        
        # 创建堆叠部件
        self.stacked_widget = QStackedWidget()
        
        # 添加功能页面
        self.stacked_widget.addWidget(ImageProcessor())
        self.stacked_widget.addWidget(BackgroundRemover())
        
        content_layout.addWidget(self.stacked_widget)
        
        # 将左右面板添加到主布局
        main_layout.addWidget(nav_panel)
        main_layout.addWidget(content_panel)
        
        # 设置默认大小
        self.resize(1400, 800)
    
    def show_page(self, index, clicked_btn):
        """显示指定的功能页面并更新按钮状态"""
        # 更新按钮状态
        for btn in self.nav_buttons:
            btn.setChecked(btn == clicked_btn)
        
        # 切换页面
        self.stacked_widget.setCurrentIndex(index)